#!/bin/sh


if [ $# != 3 ] ; then 
 echo "USAGE: $0 need fromServerId and targetServerId and mergelistString" 
 exit 1; 
fi 
dir="/server/script" 

#/server/script/stop_gs.sh gs_$1
#/server/script/stop_gs.sh gs_$2

#cd /server/gameserver_$1/script
#/server/gameserver_$1/script/start_gs.sh 
#cd /server/gameserver_$2/script
#/server/gameserver_$2/script/start_gs.sh 



echo "备份数据库"
cd $dir
/server/script/db/databackup.sh $1
/server/script/db/databackup.sh $2

#Param="?from="$1"&to="$2
#echo "Param"$Param
#links  http://114.55.55.101/monitor/reignonline/cli/merge.php$Param
#links  http://123.206.58.114/monitor/reignonline/cli/merge.php$Param
srcgamedb="reigngamedata_dev_"$1
srcgamelog="reigngamelog_dev_"$1

destgamedb="reigngamedata_dev_"$2
destgamelog="reigngamelog_dev_"$2

#echo "按任意键继续"
#read -n 1
 
host="127.0.0.1"  
PORT="3306"
user="root"  
passwd="root"  



File="src_gamedb.sql"
Filelog="src_gamelog.sql"
Dbtables="achievement army_data blacklist_data equip_insure everyday everyday_reward everydaypay_reward fans_data flower gm_cmd item_Exp items league_data league_pk_state player_gamedata mail_table pay_related_gold pay_related_info salesroom_info seven_star_info seven_star_total sha_cheng_war_battle_table sha_cheng_war_league_table shop_pack valentine vip_times"
mysqldump -t -uroot -p'root' $srcgamedb $Dbtables> $File
mysqldump -t -uroot -p'root' $destgamelog "log_pay_record" > $Filelog


#mysql -h$host -u$user -p$passwd $destgamedb < $File 
mysql -h$host -u$user -p$passwd $destgamelog < $Filelog 

echo "合服目录备份数据库"
cd /server/merge
./log_saved.sh $1
./now_db_saved.sh $1
./log_saved.sh $2
./now_db_saved.sh $2

echo "修改合服配置"
cd /server/merge/config


sed -i  's/src_db_name, \"reigngamedata_dev_\([0-9]\)\+\"/src_db_name, \"reigngamedata_dev_'$1'\"/g' mergeServer.config

sed -i  's/des_db_name, \"reigngamedata_dev_\([0-9]\)\+\"/des_db_name, \"reigngamedata_dev_'$2'\"/g' mergeServer.config

sed -i  's/src_name_append,\"\.s\([0-9]\)\+\"/src_name_append,\"\.s'$1'\"/g' mergeServer.config

sed -i  's/des_name_append,\"\.s\([0-9]\)\+\"/des_name_append,\"\.s'$2'\"/g' mergeServer.config



echo 'modify config'
curdata=`date "+%Y-%m-%d"`
curtime=`date -d "$curdata 00:00:00" +%s`

cd /server/gameserver_$2/config

sed -i  's/serverid_other,\[\([0-9]\|,\)*\]/serverid_other,\['$3'\]/g' server.config

sed -i  's/server_merge_timestamp, \([0-9]\)\+/server_merge_timestamp, '$curtime'/g' server.config
sed -i  's/server_merge_timestamp,\([0-9]\)\+/server_merge_timestamp, '$curtime'/g' server.config

cd /server/merge
./start_merge_auto.sh
#/server/gameserver_$2/script/start_gs.sh 

echo 'process over'
